package com.gitee.ixtf.rsocket.guice

import com.gitee.ixtf.rsocket.BrokerAuth
import com.google.inject.AbstractModule
import com.google.inject.Provides
import com.google.inject.Singleton
import io.vertx.core.Vertx
import io.vertx.ext.auth.jwt.JWTAuth
import io.vertx.kotlin.ext.auth.jwt.jwtAuthOptionsOf
import io.vertx.kotlin.ext.auth.pubSecKeyOptionsOf

open class RSocketGuice : AbstractModule() {
  @BrokerAuth
  @Singleton
  @Provides
  private fun JWTAuth(vertx: Vertx): JWTAuth = buildBrokerAuth(vertx)

  protected open fun buildBrokerAuth(vertx: Vertx): JWTAuth {
    val buffer = "__com.gitee.ixtf.rsocket__"
    val pubSecKey = pubSecKeyOptionsOf(algorithm = "HS256").setBuffer(buffer)
    return JWTAuth.create(vertx, jwtAuthOptionsOf(pubSecKeys = listOf(pubSecKey)))
  }
}
